package com.huangge1199.codewars;

import java.util.Arrays;

/**
 * @ClassName Kata1
 * @Description TODO
 * @Author huangge1199
 * @Date 2021/10/15 14:04
 **/
public class Kata1 {
    public static long nextBiggerNumber(long n) {
        String str = "" + n;
        for (int i = str.length() - 2; i >= 0; i--) {
            if (str.charAt(i) < str.charAt(i + 1)) {
                char[] chs = str.substring(i).toCharArray();
                char chi = str.charAt(i);
                Arrays.sort(chs);
                str = str.substring(0, i);
                String temp = "";
                for (char ch : chs) {
                    if (str.length() == i && ch > chi) {
                        str += ch;
                    } else {
                        temp += ch;
                    }
                }
                return Long.parseLong(str + temp);
            }
        }
        return -1;
    }
}
